package com.cookbook.cookbookservice.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;

/**
 * @program: mid-tourism-service
 * @ClassName TencentCOS
 * @description:
 * @author: MiaoChao
 * @create: 2024-09-02 17:59
 * @Version 1.0
 **/
@Component

public class TencentCOS {
    private Logger logger = LoggerFactory.getLogger(TencentCOS.class);

    private String secretId = "AKIDw479A2udEhCYtpvtiNrB8rHJzt0vx6Lg";//用户的 SecretId，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
    private String secretKey = "XCvCbnjs2mihuP75mbYuY2kYdBlYQ4xR";//用户的 SecretKey，建议使用子账号密钥，授权遵循最小权限指引，降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
    private String bucketName = "suixinyou-1309452461";
    private String regionName = "ap-nanjing";

    private String dir = "cookbook";

    private COSCredentials cred;

    private Region region;

    private ClientConfig clientConfig;

    private COSClient cosClient;

    public COSCredentials getCred() {
        return cred;
    }

    public void setCred(COSCredentials cred) {
        this.cred = cred;
    }

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public ClientConfig getClientConfig() {
        return clientConfig;
    }

    public void setClientConfig(ClientConfig clientConfig) {
        this.clientConfig = clientConfig;
    }

    public COSClient getCosClient() {
        return cosClient;
    }

    public void setCosClient(COSClient cosClient) {
        this.cosClient = cosClient;
    }

    @PostConstruct
    public void postConstructMethod() {
        cred = new BasicCOSCredentials(secretId, secretKey);
        region = new Region(regionName);
        clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        cosClient = new COSClient(cred, clientConfig);
    }

    public String uploadCOS(String localFilePath) {
        File localFile = new File(localFilePath);
        String key = localFile.getName();
        String path = dir + "/" + key;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        return path;
    }

    public String uploadCOS(MultipartFile file) {
        String key = file.getName();
        String path = dir + "/" + key;
        PutObjectRequest putObjectRequest = null;
        try {
            // 创建上传Object的Metadata
            ObjectMetadata meta = new ObjectMetadata();

            // 必须设置ContentLength
            meta.setContentLength(file.getSize());
            putObjectRequest = new PutObjectRequest(bucketName, path, file.getInputStream(), meta);
        } catch (IOException e) {
            logger.error("上传失败", e);
        }
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        return path;
    }
}